﻿namespace FallingWordsGame
{
    using System;

    public class GameManager
    {
        private const string START = "START";
        private const string OPTIONS = "OPTIONS";
        private const string ON = "ON";
        private const string OFF = "OFF";

        private const string START_SCREEN = "START_SCREEN";
        private const string OPTIONS_SCREEN = "OPTIONS_SCREEN";

        private bool gameStarted;

        private string onScreen;
        private string highlited;

        private Keyboard keyboard;

        public GameManager(Keyboard keyboard)
        {
            this.keyboard = keyboard;

            this.onScreen = GameManager.START_SCREEN;
            this.highlited = GameManager.START;
        }

        public void Run()
        {
            keyboard.EnterPressed += (sender, eventInfo) =>
            {
                this.StartIFPossible();
                this.ShowScreen();
            };

            this.ShowScreen();
            
            while (!gameStarted)
            {
                this.keyboard.ProcessInput();
            }
            
            Console.Clear();
        }

        private void ShowScreen()
        {
            Console.Clear();
            if(GameManager.START_SCREEN == this.onScreen)
            {
                this.StartScreen();
            }
            else if (GameManager.OPTIONS_SCREEN == this.onScreen)
            {
                this.OptionsScreen();
            }
        }

        private void StartIFPossible()
        {
            if(this.highlited == GameManager.OPTIONS)
            {
                this.onScreen = GameManager.OPTIONS_SCREEN;
                this.highlited = GameManager.ON;
            }
            else if(this.highlited == GameManager.ON)
            {
                // TODO: Sound On
                this.highlited = GameManager.OPTIONS;
                this.onScreen = GameManager.START_SCREEN;
            }
            else if( this.highlited == GameManager.OFF)
            {
                // TODO: Sound Off
                this.highlited = GameManager.OPTIONS;
                this.onScreen = GameManager.START_SCREEN;
            }
            else if (this.highlited == GameManager.START)
            {
                this.gameStarted = true;
            }
        }

        public void OnArrowPress(ConsoleKey key)
        {
            switch(key)
            {
                case ConsoleKey.UpArrow:
                    this.HandleUpDownArrowCommand();
                    break;
                case ConsoleKey.DownArrow:
                    this.HandleUpDownArrowCommand();
                    break;
                case ConsoleKey.LeftArrow:
                    this.HandleLeftRightArrowCommand();
                    break;
                case ConsoleKey.RightArrow:
                    this.HandleLeftRightArrowCommand();
                    break;
            }
            this.ShowScreen();
        }

        private void HandleLeftRightArrowCommand()
        {
            if(this.onScreen == GameManager.OPTIONS_SCREEN)
            {
                if(this.highlited == GameManager.OFF)
                {
                    this.highlited = GameManager.ON;
                }
                else
                {
                    this.highlited = GameManager.OFF;
                }
            }
        }

        private void HandleUpDownArrowCommand()
        {
            if(this.onScreen == GameManager.START_SCREEN)
            {
                if(this.highlited == GameManager.START)
                {
                    this.highlited = GameManager.OPTIONS;
                }
                else
                {
                    this.highlited = GameManager.START;
                }
            }
        }

        private void StartScreen()
        {
            // TODO: Refactor Extract Constants

            int width = Console.WindowWidth;
            
            Console.SetCursorPosition(width - 3, 8);

            if (this.onScreen == GameManager.START_SCREEN && this.highlited == GameManager.START)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                
                Console.WriteLine(GameManager.START.PadLeft(44, ' '));
                Console.ForegroundColor = ConsoleColor.White;
                Console.SetCursorPosition(width - 2, 10);
                Console.WriteLine(GameManager.OPTIONS.PadLeft(44, ' '));
            }
            else if (this.onScreen == GameManager.START_SCREEN && this.highlited == GameManager.OPTIONS)
            {
                Console.WriteLine(GameManager.START.PadLeft(44, ' '));
                Console.ForegroundColor = ConsoleColor.Red;
                Console.SetCursorPosition(width - 2, 10);
                Console.WriteLine(GameManager.OPTIONS.PadLeft(44, ' '));
                Console.ForegroundColor = ConsoleColor.White;
            }
            
            
        }

        private void OptionsScreen()
        {
            int width = Console.WindowWidth;

            Console.SetCursorPosition(width - 3, 8);
            Console.Write("SOUND: ".PadLeft(38, ' '));
                
            if (this.onScreen == GameManager.OPTIONS_SCREEN && this.highlited == GameManager.ON)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write(GameManager.ON);
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(GameManager.OFF.PadLeft(6, ' '));
            }
            else if (this.onScreen == GameManager.OPTIONS_SCREEN && this.highlited == GameManager.OFF)
            {
                Console.Write(GameManager.ON);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(GameManager.OFF.PadLeft(6, ' '));
                Console.ForegroundColor = ConsoleColor.White;

            }
        }
    }
}
